package ua.kpi.ipsa.expert.api.db;

import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.DatabaseEntry;

public class AssignedAlgorithmTuple {

	public int codePortionPFK;
	public int algorithmPFK;
	public boolean finished;

	public AssignedAlgorithmTuple() {
	}

	public AssignedAlgorithmTuple(int codePortionPFK, int algorithmPFK) {
		this.codePortionPFK = codePortionPFK;
		this.algorithmPFK = algorithmPFK;
	}

	public AssignedAlgorithmTuple(int codePortionPFK, int algorithmPFK,
			boolean finished) {
		this.codePortionPFK = codePortionPFK;
		this.algorithmPFK = algorithmPFK;
		this.finished = finished;
	}

	AssignedAlgorithmTuple(DatabaseEntry key, DatabaseEntry data) {
		setKeyEntry(key);
		setDataEntry(data);
	}

	DatabaseEntry getKeyEntry() {
		DatabaseEntry entry = new DatabaseEntry();
		TupleOutput out = new TupleOutput();
		out.writeInt(codePortionPFK);
		out.writeInt(algorithmPFK);
		entry.setData(out.getBufferBytes(), out.getBufferOffset(),
				out.getBufferLength());
		return entry;
	}

	DatabaseEntry getValueEntry() {
		DatabaseEntry entry = new DatabaseEntry();
		TupleOutput out = new TupleOutput();
		out.writeBoolean(finished);
		entry.setData(out.getBufferBytes(), out.getBufferOffset(),
				out.getBufferLength());
		return entry;
	}

	void setKeyEntry(DatabaseEntry entry) {
		TupleInput in = new TupleInput(entry.getData(), entry.getOffset(),
				entry.getSize());
		codePortionPFK = in.readInt();
		algorithmPFK = in.readInt();
	}

	void setDataEntry(DatabaseEntry entry) {
		TupleInput in = new TupleInput(entry.getData(), entry.getOffset(),
				entry.getSize());
		finished = in.readBoolean();
	}
}